package me.numark.view;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import me.numark.R;
import me.numark.activities.MarkDetailActivity;
import me.numark.fragments.CommentsFragment;
import me.numark.model.FeedTag;
import me.numark.model.Topic;

/**
 * Created by Evan on 15/7/24.
 */
public class TopicAdapter extends UltimateViewAdapter<TopicViewHolder> {

    List<FeedTag> dataList;
    MarkDetailActivity.OnCommentsFragmentInteractionListener mClickTopicListener;
    Object mParent;
    long feedId;
    int topicType;
    public TopicAdapter(MarkDetailActivity.OnCommentsFragmentInteractionListener clickTopicListener, List<FeedTag> dataList, Object parent,long feedId,int topicType) {
        this.dataList = dataList;
        this.feedId = feedId;
        mClickTopicListener = clickTopicListener;
        mParent = parent;
        this.topicType = topicType;
    }

    public boolean commentable = false;

    @Override
    public TopicViewHolder getViewHolder(View view) {
        return new TopicViewHolder(view, mParent, TopicViewHolder.STYLE_NONE, null,feedId,topicType);
    }

    @Override
    public TopicViewHolder onCreateViewHolder(ViewGroup parent) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.comment_list_item, parent, false);
        TopicViewHolder vh = new TopicViewHolder(v, mParent, TopicViewHolder.STYLE_NORMAL,
                commentable ? mClickTopicListener : null,feedId,topicType);
        return vh;
    }

    @Override
    public int getAdapterItemCount() {
        return dataList.size();
    }

    @Override
    public long generateHeaderId(int i) {
        return 0;
    }

    @Override
    public void onBindViewHolder(TopicViewHolder holder, int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= dataList.size() : position < dataList.size()) && (customHeaderView != null ? position > 0 : true)) {

            FeedTag ft = dataList.get(customHeaderView != null ? position - 1 : position);
                holder.bindData(ft);
            // ((ViewHolder) holder).itemView.setActivated(selectedItems.get(position, false));
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup viewGroup) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    }

    public void append(FeedTag item) {
        insert(dataList, item, dataList.size());
    }

    public void append(FeedTag[] items) {
        int pos = dataList.size();
        for (FeedTag item : items) {
            if (!dataList.contains(item))
                dataList.add(item);
        }
        notifyItemRangeInserted(pos, items.length);
    }

    public void remove(int position) {
        remove(dataList, position);
    }

    public void clear() {
        clear(dataList);
    }

}
